﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using Kwan.Core;
using Kwan.Helper.Strings;
using System.IO;
using BG.Data;
using Kwan.Data;

namespace BG.Web.Ext
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (IConnectinContext cc = DBContext.Context1)
            {
                //Response.Write("sign is " + GetRSA());
                for (int j = 4; j < 10; j++)
                {
                    for (int i = 1; i < 10; i++)
                    {
                        TRoom r = new TRoom();
                        r.RoomNO = j + "0" + i;
                        r.Status = 0;
                        r.Cid = 2;
                        r.Floor = j.ToString();
                        r.CreatedDate = DateTime.Now;
                        DBContext.Save(r);
                    }
                }
            }
        }

        

        private void SumRoomPrice()
        {
            NameValueCollection nv = new NameValueCollection();
            NameValueCollection ps = new NameValueCollection();
            ps.Add("2011-11-15 17:50:00", "2011-11-15 19:00:00"); nv.Add("正常钟点不超时", "60");
            ps.Add("2011-11-15 07:51:00", "2011-11-16 12:30:00"); nv.Add("正常一天不超时", "180");
            ps.Add("2011-11-15 07:52:00", "2011-11-18 12:30:00"); nv.Add("正常三天不超时", "540");
            ps.Add("2011-11-15 07:53:00", "2011-11-16 15:20:00"); nv.Add("正常一天超时两小时", "200");
            ps.Add("2011-11-15 07:54:00", "2011-11-18 15:20:00"); nv.Add("正常三天超时两小时", "560");
            ps.Add("2011-11-15 18:51:00", "2011-11-16 12:30:00"); nv.Add("半天不超时", "120");
            ps.Add("2011-11-15 18:52:00", "2011-11-17 12:30:00"); nv.Add("一天半不超时", "300");
            ps.Add("2011-11-15 18:53:00", "2011-11-16 15:20:00"); nv.Add("半天超时两小时", "140");
            ps.Add("2011-11-15 18:54:00", "2011-11-17 15:20:00"); nv.Add("一天半超时两小时", "320");
            ps.Add("2011-11-15 18:55:00", "2011-11-18 15:20:00"); nv.Add("两天半超时两小时", "500");
            ps.Add("2011-11-15 17:56:00", "2011-11-15 21:56:00"); nv.Add("正常钟点超时半小时", "70");
            ps.Add("2011-11-15 18:00:00", "2011-11-15 22:31:00"); nv.Add("正常钟点超时一小时", "80");
            ps.Add("2011-11-15 18:01:00", "2011-11-15 23:32:00"); nv.Add("正常钟点超时两小时", "90");
            ps.Add("2011-11-15 18:02:00", "2011-11-16 00:33:00"); nv.Add("正常钟点超时三小时", "120");
            ps.Add("2011-11-15 18:03:00", "2011-11-16 13:34:00"); nv.Add("正常一天+钟点不超时", "120");
            ps.Add("2011-11-15 20:50:00", "2011-11-15 20:34:00"); nv.Add("正常一天不超时1", "60");
            ps.Add("2011-11-15 20:51:00", "2011-11-15 13:01:00"); nv.Add("正常一天半价不超时", "60");

            for (int i = 0; i < ps.Count; i++)
            {
                DateTime checkInDate = DateTime.Parse(ps.GetKey(i));
                DateTime checkOutDate = DateTime.Parse(ps.Get(i));
                int roomPrice = 180; int roomHalfPrice = 120; int roomNightPrice = 110; int roomHourPrice = 60;

                double finalPrice = PriceManager.Instance.GetFinalPrice(checkInDate, checkOutDate, roomPrice, roomHalfPrice, roomNightPrice);

                double liveMinutes = (checkOutDate - checkInDate).TotalMinutes;

                if (liveMinutes <= 390)
                {
                    finalPrice = PriceManager.Instance.GetHourPrice(checkInDate, checkOutDate, roomPrice, roomHalfPrice, roomHourPrice);
                }

                Response.Write(string.Format(" {0} 的费用为：{1}，计费系统结果为：{2}<br/>", nv.GetKey(i), nv.Get(i), finalPrice));
            }
        }

        private string GetRSA()
        {
            string PrivateKeyPath = "Private_" + DateTime.Now.ToString("yyyyMMddHHmmss")+".xml";
            string PublicKeyPath = "Public_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xml";
            string SignPath = "Sign_" + DateTime.Now.ToString("yyyyMMddHHmmss");

            RasCrypto rsa = new RasCrypto();
            string s = rsa.GetRSAParametersData(true);
            string privateKey = rsa.PrivateKey;
            CreatetToFile(PrivateKeyPath, rsa.PrivateKey);
            CreatetToFile(PublicKeyPath, rsa.PublicKey);
            CreatetToFile(SignPath, rsa.Sign("KwanJH"));
            return privateKey;
        }

        private bool SignRSA()
        {
            string PrivateKeyPath = "Private.xml";
            string PublicKeyPath = "Public_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xml";
            string SignPath = "Sign_" + DateTime.Now.ToString("yyyyMMddHHmmss");

            RasCrypto rsa = new RasCrypto();
            rsa.PrivateKey = FileHelper.GetFileString(PrivateKeyPath);
            string publicKey = rsa.PublicKey;
            string signKey = rsa.Sign("KwanJH");
            CreatetToFile(PublicKeyPath, publicKey);
            CreatetToFile(SignPath, signKey);
            return rsa.Verify("KwanJH", signKey);
        }

        private bool VerifySign()
        {
            RasCrypto rsa = new RasCrypto();
            rsa.PublicKey = FileHelper.GetFileString("Public_20111123055909.xml");
            string signKey = FileHelper.GetFileString("Sign_20111123055909");
            return rsa.Verify("KwanJH", signKey);
        }

        /// <summary>
        /// 创建私钥文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="privatekey"></param>
        public static void CreatetToFile(string path, string privatekey)
        {
            try
            {
                string solutionPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                path = solutionPath + path;
                FileStream privatekeyxml = new FileStream(path, FileMode.Create);
                StreamWriter sw = new StreamWriter(privatekeyxml);
                sw.WriteLine(privatekey);
                sw.Close();
                privatekeyxml.Close();
            }
            catch
            {
                throw;
            }
        }
    }
}